package com.tm.mygsyplayer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.tm.mygsyplayer.R;

public class ShortVideoPlayer extends StandardGSYVideoPlayer implements VideoAllCallBack {

    private static final String TAG = "ShortVideoPlayer";
    private ImageView ivStartPause;

    public ShortVideoPlayer(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public ShortVideoPlayer(Context context) {
        super(context);
    }

    public ShortVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getLayoutId() {
        return R.layout.empty_control_video;
    }

    @Override
    protected void init(Context context) {
        super.init(context);

        setVideoAllCallBack(this);
        ivStartPause = findViewById(R.id.iv_start_pause);

        ivStartPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startOrPause();
            }
        });
    }

    @Override
    protected void changeUiToNormal() {
        super.changeUiToNormal();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);
        ivStartPause.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void changeUiToPreparingShow() {
        super.changeUiToPreparingShow();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);
        ivStartPause.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void changeUiToPrepareingClear() {
        super.changeUiToPrepareingClear();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);

    }

    @Override
    protected void changeUiToPauseShow() {
        super.changeUiToPauseShow();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);
        ivStartPause.setVisibility(View.VISIBLE);
    }

    @Override
    protected void changeUiToPauseClear() {
        super.changeUiToPauseClear();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);

    }

    @Override
    protected void changeUiToPlayingBufferingShow() {
        super.changeUiToPlayingBufferingShow();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);

        ivStartPause.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void changeUiToCompleteShow() {
        super.changeUiToCompleteShow();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);

        ivStartPause.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void changeUiToError() {
        super.changeUiToError();
        setViewShowState(mTopContainer, INVISIBLE);
        setViewShowState(mBottomContainer, INVISIBLE);

    }

    @Override
    protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY);
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false;

        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false;

        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false;

    }

    @Override
    protected void touchDoubleUp() {
//        super.touchDoubleUp();
        //不需要双击暂停
    }


    @Override
    public void onStartPrepared(String url, Object... objects) {

    }

    @Override
    public void onPrepared(String url, Object... objects) {

    }

    @Override
    public void onClickStartIcon(String url, Object... objects) {

    }

    @Override
    public void onClickStartError(String url, Object... objects) {

    }

    @Override
    public void onClickStop(String url, Object... objects) {

    }

    @Override
    public void onClickStopFullscreen(String url, Object... objects) {

    }

    @Override
    public void onClickResume(String url, Object... objects) {

    }

    @Override
    public void onClickResumeFullscreen(String url, Object... objects) {

    }

    @Override
    public void onClickSeekbar(String url, Object... objects) {

    }

    @Override
    public void onClickSeekbarFullscreen(String url, Object... objects) {

    }

    @Override
    public void onAutoComplete(String url, Object... objects) {

    }

    @Override
    public void onEnterFullscreen(String url, Object... objects) {

    }

    @Override
    public void onQuitFullscreen(String url, Object... objects) {

    }

    @Override
    public void onQuitSmallWidget(String url, Object... objects) {

    }

    @Override
    public void onEnterSmallWidget(String url, Object... objects) {

    }

    @Override
    public void onTouchScreenSeekVolume(String url, Object... objects) {

    }

    @Override
    public void onTouchScreenSeekPosition(String url, Object... objects) {

    }

    @Override
    public void onTouchScreenSeekLight(String url, Object... objects) {

    }

    @Override
    public void onPlayError(String url, Object... objects) {

    }

    @Override
    public void onClickStartThumb(String url, Object... objects) {

    }

    /**
     * //点击了播放中的空白区域，objects[0]是title，object[1]是当前所处播放器（全屏或非全屏）
     *
     * @param url
     * @param objects
     */
    @Override
    public void onClickBlank(String url, Object... objects) {
        Toast.makeText(mContext, "onClickBlank", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "mCurrentState:" + mCurrentState);
        startOrPause();
    }

    private void startOrPause() {
        if (mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR) {
            if (isShowNetConfirm()) {
                showWifiDialog();
                return;
            }
            startButtonLogic();

            ivStartPause.setVisibility(View.INVISIBLE);
        } else if (mCurrentState == CURRENT_STATE_PLAYING) {

            try {
                onVideoPause();
            } catch (Exception e) {
                e.printStackTrace();
            }
            setViewShowState(mStartButton, VISIBLE);
            setStateAndUi(CURRENT_STATE_PAUSE);

            ivStartPause.setVisibility(View.VISIBLE);
        } else if (mCurrentState == CURRENT_STATE_PAUSE) {
            setViewShowState(mStartButton, INVISIBLE);
            if (!mHadPlay && !mStartAfterPrepared) {
                startAfterPrepared();
            }

            try {
                getGSYVideoManager().start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            setStateAndUi(CURRENT_STATE_PLAYING);

            ivStartPause.setVisibility(View.INVISIBLE);
        } else if (mCurrentState == CURRENT_STATE_AUTO_COMPLETE) {
            startButtonLogic();

            ivStartPause.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClickBlankFullscreen(String url, Object... objects) {

    }

}
